<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\common\model;
use think\Validate;
use think\model\concern\SoftDelete;
use think\facade\Log;

class ViewGetClasses extends Common
{ 
    /**
     * @param $post
     *
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function tableData($post)
    {
        $tableWhere = $this->tableWhere($post);
        $list = $this->field($tableWhere['field'])->where($tableWhere['where'])->paginate(1000);
        $data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
        
        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = count($data);
        $re['data'] = $data;

        return $re;
    }
 
    //where搜索条件
    protected function tableWhere($post)
    {
        $where = [];
        if (isset($post['bmdm']) && $post['bmdm'] != "") {
            $where[] = ['bmdm', 'eq', $post['bmdm']];
        }

        if (isset($post['zydm']) && $post['zydm'] != "") {
            $where[] = ['zydm', 'eq', $post['zydm']];
        }

        if (isset($post['plan_year']) && $post['plan_year'] != "") {
            $where[] = ['plan_year', 'eq', $post['plan_year']];
        }

        if (isset($post['plan_season']) && $post['plan_season'] != "") {
            $where[] = ['plan_season', 'eq', $post['plan_season']];
        }

        if (isset($post['plan_grade']) && $post['plan_grade'] != "") {
            $where[] = ['plan_grade', 'eq', $post['plan_grade']];
        }

        if (isset($post['plan_degree_id']) && $post['plan_degree_id'] != "") {
            $where[] = ['plan_degree_id', 'eq', $post['plan_degree_id']];
        }

        $result['where'] = $where;
        $result['field'] = "*";
        return $result;
    }

    /**
     * @param $list
     *
     * @return mixed
     */
    protected function tableFormat($list)
    {
        foreach ($list as $key => $val) {
            if ($val) {
                Log::Info($val);
                if ($val['plan_dir'] == "2") {
                    $list[$key]['plan_class_name'] = $list[$key]['plan_class_name'].'_升学';
                } else if ($val['plan_dir'] == "3") {
                    $list[$key]['plan_class_name'] = $list[$key]['plan_class_name'].'_创业';
                }
            }
        }
        return $list;
    }
}